<template>
  <div>
    <!-- <message-index type="error" :message="error.message" v-if="error.status"></message-index> -->
    <loader-ing v-if="isLoading" text="在加载了"></loader-ing>
    <global-header :user="currentUser"></global-header>
    <!-- <uploader-file action="/upload" :beforeUpload="beforeUpload" @file-uploaded="onFileUploaded">
      <h2>点击上传</h2>
      <template #loading>
        <div class="spinner-boder" role="status">
          <span class="sr-only">正在上传</span>
        </div>
      </template>
      <template #uploaded="dataProps">
        <img :src="dataProps.uploadedData.data.url" alt="" width="500">
      </template>
    </uploader-file> -->
    <router-view></router-view>
    <footer class="text-center py-4 text-secondary bg-light mt-6">
      <small>
        <ul class="list-inline mb-0">
          <li class="list-inline-item">专栏</li>
          <li class="list-inline-item">文章</li>
          <li class="list-inline-item">详情</li>
          <li class="list-inline-item">地址</li>
          <li class="list-inline-item">联系</li>
        </ul>
      </small>
    </footer>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, onMounted, watch } from "vue";
import GlobalHeader from "./components/GlobalHeader.vue";
import { useStore } from "vuex";
import LoaderIng from "./components/LoaderIng.vue";
import { GolbalDataProps, ImageProps,ResponseType } from "./store";
import axios from "axios";
import createMessage from "./hooks/createMessage";
// import MessageIndex from "./components/MessageIndex.vue";
// import UploaderFile from "./components/UploaderFile.vue";
import { file } from "@babel/types";
export default defineComponent({
  name: "App",
  components: {
    GlobalHeader,
    LoaderIng
    // MessageIndex,
    // UploaderFile
  },
  setup() {
    const store = useStore<GolbalDataProps>()
    const currentUser = computed(() => store.state.user)
    const isLoading = computed(() => store.state.loading)
    const token = computed(() => store.state.token)
    const error = computed(() => store.state.error)
    onMounted(() => {
      if(!currentUser.value.isLogin && token.value) {
        //携带token
        axios.defaults.headers.common.Authorization = `Bearer ${token.value}`
        //再次获取用户信息
        store.dispatch('fetchCurrentUser')
      }
      watch(() => error.value.status, () => {
        const { status, message } = error.value
        if(status && message) {
          createMessage(message, 'error')
        }
      })
    })
    // const beforeUpload = (file: File) => {
    //   const isJPG = file.type === 'image/jpeg'
    //   if(!isJPG) {
    //     createMessage('上传图片只能是 JPG 格式', 'error')
    //   }
    //   return isJPG
    // }
    // const onFileUploaded = (rawData: ResponseType<ImageProps>) => {
    //   createMessage(`上传图片 ID ${rawData.data._id}`,'success')
    // }
    return {
      currentUser,
      isLoading,
      error
      // beforeUpload,
      // onFileUploaded
    };
  },
});
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;
}

nav a {
  font-weight: bold;
  color: #2c3e50;
}

nav a.router-link-exact-active {
  color: #42b983;
}
</style>
